---
title: "Replication File ReadMe"
author: "Nicholas Kuipers, Gareth Nellis, and Michael Weaver"
date: "July 16, 2019"
output: html_document
---

```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
```

## *Does Electing Extremist Parties Increase Violence and Intolerance?*

We very much welcome replications of this work. Please reach out to the authors at any time to ask questions, request additional data, or point out errors.

# Using this Replication File

This file allows the user to reproduce any analysis referenced in the paper. In order to do this, users should take the following steps. Once steps `2` and `3` are completed, running the entirety of [main.R](./main.R) will reproduced all of the analyses in the paper and supplementary appendix.

1. Open the main replication script file: [main.R](./main.R)
2. On line `5`, set the working directory as the folder containing the replication folder downloaded from the BJPS Dataverse.
3. Install the packages required (lines `20` to `42`).
4. Before running **any** commands, run the script on lines `11` to `17` ([./make_directory.R](./make_directory.R)), which will reorganize the data into directories. This is required for data and script files to be found correctly.
4. You must run the scripts loading functions created for this replication file on line `45`.
4. To prepare all data required for the analyses, you must run lines `55` to `182`. This produces data for analysis from the raw data we originally received, except in the instances noted.
5. Figures and Tables from the main paper and the supplementary appendix have their own scripts that produce them as labeled in [main.R](./main.R). These will only function after the preceding steps are completed. 
6. `main.R` will place the resulting tables and figures in:
  * PDFs of figures from the paper and SA will be saved to [**output/figures**](./output/figures).
  * TEX files for tables from the paper and SA will be saved to [**output/tables**](./output/tables).

## Data Files [./data](./data)

| Description | Location in replication file | Original source | Notes |
|-|-|--|-|
| DPRD-II elections data (2004, 2009) | [./data/elections/dprd2_2004/dprd2_2004_results.csv](./data/elections/dprd2_2004/dprd2_2004_results.csv); [./data/elections/dprd2_2009/dprd2_2009_results.csv](./data/lections/dprd2_2009/dprd2_2009_results.csv) | Indonesian Electoral Commission (KPU); collected from Jakarta office by authors | Digitized by authors with assistance from Mechanical Turk workers |
| Village Potential Statistics/PODES (2003, 2008, 2014) | Raw data proprietary; aggregated data: [./data/PODES/podes_balance_analysis.csv](./data/PODES/podes_balance_analysis.csv);[./data/PODES/podes_main_analysis.csv](./data/PODES/podes_main_analysis.csv) | Indonesian Statistics Agency (BPS) https://library.duke.edu/data/sources/podes (gated) | Aggregated data can be reproduced using raw PODES files as well as  [./scripts/make/make_podes_data.R](./scripts/make/make_podes_data.R) and [./scripts/make/make_pretreat_podes_data.R](./scripts/make/make_pretreat_podes_data.R)|
| National Violence Monitoring System (annual, 1997 to 2014) | in [./data/NVMS](./data/NVMS) `DATA SNPK YYYY_JRI 30 11 2015.dta` [YYYY takes all integer values 1997 to 2014] | https://microdata.worldbank.org/index.php/catalog/2628 |  |
| Indonesia Family Life Survey (2007, 2014) | [./data/surveys/ifls_individual_data_07_14.csv](./data/surveys/ifls_individual_data_07_14.csv); [./data/surveys/ifls_commleader_data_07_14.csv](./data/surveys/ifls_commleader_data_07_14.csv) | https://www.rand.org/well-being/social-and-behavioral-policy/data/FLS/IFLS.html |  |
| Candidates biographical data (2019) | [./data/candidates/full_data_merged_w_codes021119.csv](./data/candidates/full_data_merged_w_codes021119.csv) | https://infopemilu.kpu.go.id/pileg2019/pencalonan/ | Scraped by authors from the Indonesian Electoral Commission’s website in February 2019 |
| Village crosswalks (annual, 1997 to 2014) | Proprietary | Indonesian Statistics Agency (BPS) | The village-level crosswalks were provided to the authors by Samuel Bazzi (Boston University). |
| Kecamatan crosswalks | [./data/crosswalks/](/data/crosswalks) | Author-generated links between electoral data and other data sources (several files) | The kecamatan crosswalks can be produced using the data from Samuel Bazzi and [./scripts/make/kecamatan_crosswalks.R](./scripts/make/kecamatan_crosswalks.R).  |
| Sharia laws (annual, 1998 to 2013) | [./data/sharia_laws/kabs_sharia_laws_011218.csv](./data/sharia_laws/kabs_sharia_laws_011218.csv) | Michael Buehler, The Politics of Shari’a Law: Islamist Activists and the State in Democratizing Indonesia (Cambridge University Press, 2016), Appendix 1 |  |
| Indonesia Population Census (2000, 2010) | [./data/census_2000_ethnicity/00-data/final/20190128_eth_rel_kec_data.csv](./data/census_2000_ethnicity/00-data/final/20190128_eth_rel_kec_data.csv) | https://dataverse.harvard.edu/dataset.xhtml?persistentId=doi:10.7910/DVN/ZDYXYI | Village codes were merged in by the authors by matching on unique values of other covariates and triangulating with other data sources |
| Mayor/regent party endorsements (annual, 2005 to 2008) | [./data/mayors/mayors_dates_corrected.csv](./data/mayors/mayors_dates_corrected.csv) |  | Collected and provided by Priya Mukherjee (College of William & Mary) |
| District budgets (2008, 2013) | [./data/budget/budget_2008_fin.csv](./data/budget/budget_2008_fin.csv);[./data/budget/budget_2013_fin.csv](./data/budget/budget_2013_fin.csv) | Indonesian Ministry of Finance http://www.djpk.kemenkeu.go.id/?p=5412 |  |


## Script Files

1. [./main.R](./main.R): This file, when run, will reproduce the entirety of the data and analyses reported in the paper and supplementary appendix.
2. [./make_directory.R](./make_directory.R): This file (run by `main.R`) will reorganize the files downloaded from the Dataverse into the directory structure used by this replication file.
3. [./scripts/functions.R](./scripts/functions.R): This file contains custom functions used in this replication file.
4. Files to make data:
  - [./scripts/make/kecamatan_crosswalks.R](./scripts/make/kecamatan_crosswalks.R): This file, when combined with the data supplied by Samuel Bazzi, can be used to create several crosswalks linking kecamatan data from several sources to the 2004 and 2009 electoral constituencies. This is currently unused in the replication file; instead the completed crosswalks are included.
  - [./scripts/make/make_elections_data.R](./scripts/make/make_elections_data.R): This file creates the 2004 and 2009 elections data used as the treatment in the main analyses and to check balance.
  - [./scripts/make/make_fractionalization_data.R](./scripts/make/make_fractionalization_data.R): This file creates the ethnic/religious fractionalization data used in balance tests.
  - [./scripts/make/make_ifls_data.R](./scripts/make/make_ifls_data.R): This file prepares the subset of the IFLS survey data we use for analysis.
  - [./scripts/make/make_podes_data.R](./scripts/make/make_podes_data.R): This file aggregates the raw PODES data to constituency-level values used in this analysis. It is unused in this replication file, as the proprietary PODES data is not included.
  - [./scripts/make/make_pretreat_podes_data.R](./scripts/make/make_pretreat_podes_data.R): This file aggregates the raw PODES data to constituency-level values used in balance tests. It is unused in this replication file, as the proprietary PODES data is not included.
  - [./scripts/make/make_violence_data.R](./scripts/make/make_violence_data.R): This file aggregates the raw NVMS violence data to constituency-level values used in the analyses.
5. Files to replicate analyses: [./scripts/analyses](./scripts/analyses) contains the script files used to produce each figure and table reported in the paper and supplementary appendix. They are named with respect to the figure/table they produce. These are called by [./main.R](./main.R) and cannot be run individually without first following the instructions given in the "Using This Replication File" section of this document.  



## R Packages

The analysis for this paper was produced using `R 3.4.4` on a machine running `Ubuntu 16.04.3` with 64GB of RAM (but authors can confirm this will run with as little as 8GB of RAM). The packages used (and their versions) are given below. This may exclude some dependencies.

```{r  echo=FALSE, results = 'asis', message = F}
require(knitr)
packages = c("data.table","foreign","igraph","stringr","MASS","multiwayvcov","lmtest","stargazer","Exact","Matching","ggplot2","ri","nnet","psych","grid","gridExtra","lfe","sandwich","interflex","dplyr","magrittr", 'zoo', 'parallel')

p_sorted = sort(packages)

package_list = data.frame(Package = p_sorted, Version = unlist(lapply(p_sorted, function(x) packageDescription(x)$Version)))

kable(package_list, caption = "List of R Packages Used")
```
